﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IBM.Data.DB2;
using CourseManager.Domain.Entities;
using CourseManager.Domain.Repositories;

namespace CourseManager.WebUI.Controllers
{
    public class CourseController : Controller
    {
        CourseRepository CR;
       
       [HttpGet]
        public ActionResult GetCourseByName()
        {
            return View();
        }

        [HttpGet]
       public ActionResult GetCourse()
       {
           return View();
       }

        [HttpPost]
        public ActionResult GetCourse(FormCollection pId)
        {
            if (pId["CourseId"].Trim().Length == 0)
            {
                ModelState.AddModelError("CourseId", "Requerido");
                return View();
            }
               
            IEnumerable<Course> CourseList;
            CR = new CourseRepository();
            CourseList = CR.getCourse(Convert.ToInt32(pId["CourseId"]));
            return View(CourseList);
            
        }

      
        [HttpPost]
        public ActionResult GetCourseByName(FormCollection pName)
        {
            if (pName["CourseName"].Trim().Length == 0)
            {
                ModelState.AddModelError("CourseName", "Requerido");
                return View();
                
            }
            IEnumerable<Course> CourseList;
            CR = new CourseRepository(); 
            CourseList = CR.getCourse(Convert.ToString(pName["CourseName"]));
            return View(CourseList); 
        }

        [HttpGet]
        public ActionResult CourseDetails(int pId)
        {
            Course Course = new Course();
            CourseRepository CR = new CourseRepository();
            Course = CR.getCourseById(pId);
            return View(Course);
        }
        
        [HttpGet]
        public ActionResult AddCourse()
        {
            ViewData["InstructorList"] = GetInstructor();
            return View();
        
        }

        [HttpPost]
        public ActionResult AddCourse(FormCollection pCourse)
        {
            CR = new CourseRepository();
            Course Course = new Course();

            if (pCourse["Name"].Trim().Length == 0)
            {
                ModelState.AddModelError("Name", "Requerido");
                ViewData["InstructorList"] = GetInstructor();
                return View();

            }

            else if (pCourse["Duration"].Trim().Length == 0)
            {
                ModelState.AddModelError("Duration", "Requerido");
                ViewData["InstructorList"] = GetInstructor();
                return View();

            }

            else if (pCourse["StartDate"].Trim().Length == 0)
            {
                ModelState.AddModelError("StartDate", "Requerido");
                ViewData["InstructorList"] = GetInstructor();
                return View();

            }
            else if (pCourse["EndDate"].Trim().Length == 0)
            {
                ModelState.AddModelError("EndDate", "Requerido");
                ViewData["InstructorList"] = GetInstructor();
                return View();

            }
            else if (pCourse["Instructor"].Trim().Length == 0)
            {
                ModelState.AddModelError("Instructor", "Requerido");
                ViewData["InstructorList"] = GetInstructor();
                return View();

            }
            else if (pCourse["Description"].Trim().Length == 0)
            {
                ModelState.AddModelError("Description", "Requerido");
                ViewData["InstructorList"] = GetInstructor();
                return View();

            }
            
            Course.Name = pCourse["Name"].ToString();
            Course.Description = pCourse["Description"].ToString();
            Course.Duration = Convert.ToInt32( pCourse["Duration"]);
            Course.StartDate =Convert.ToDateTime(pCourse["StartDate"]);
            Course.EndDate  = Convert.ToDateTime(pCourse["EndDate"]);
            Course.InstructorId = Convert.ToInt32(pCourse["Instructor"]);
            ViewData["InstructorList"] = GetInstructor();
            CR.AddCourse(Course);
            return View();
        }

        public ActionResult UpdateCourse(FormCollection pCourse)
        {
            CR = new CourseRepository();
            Course Course = new Course();

            Course.Name = pCourse["Name"].ToString();
            Course.Description = pCourse["Description"].ToString();
            Course.Duration = Convert.ToInt32(pCourse["Duration"]);
            Course.StartDate = Convert.ToDateTime(pCourse["StartDate"]);
            Course.EndDate = Convert.ToDateTime(pCourse["EndDate"]);
            Course.InstructorId = Convert.ToInt32(pCourse["Instructor"]);

            CR.UpdateCourse(Course);
            return View();
        }

        public bool DeleteCourse(int pId)
        {
            CR = new CourseRepository();
            CR.DeleteCourse(pId);
            return true; 
        }

        private List<SelectListItem> GetInstructor()
        {
            InstructorRepository instructor = new InstructorRepository();
           
            var Result = (from I in instructor.GetInstructor()
                          select new SelectListItem
                          {
                              Text = I.Name,
                              Value = I.Id.ToString()

                          }).ToList();


            return Result;

        }
    }
}
